
using System;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using MIMS.WRS.Entities;
using MIMS.WRS.Data;
    
namespace MIMS.WRS.Business
{   
	/// <summary>
	/// 
	/// </summary> 
    public class ResponsibleOfficeService
    {
		public static void UpdateResponsibleOfficeBatch(ResponsibleOfficeCollection responsibleOffices, int conPointID)
		{            
			try
			{
				using (TransactionScope scope = new TransactionScope()) 
				{
					ResponsibleOfficeDAO responsibleOfficeDAO = new ResponsibleOfficeDAO();
					responsibleOfficeDAO.DeleteResponsibleOfficeByConPoint(conPointID);
					foreach (ResponsibleOffice responsibleOffice in responsibleOffices) 
					{
						responsibleOfficeDAO.CreateResponsibleOffice(responsibleOffice);
					}					

					scope.Complete();
				}
				
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessUpdateResponsibleOfficeException, ex);
			}
		} 

		public static void CreateResponsibleOfficeBatch(ResponsibleOffice responsibleOffice, int conPointID)
		{            
			try
			{				
				using (TransactionScope scope = new TransactionScope()) 
				{
					int responsibleID = responsibleOffice.ResponsibleID;

					if (responsibleID == Nulls.NullInteger) 
					{
						ResponsibleDAO responsibleDAO = new ResponsibleDAO();
						Responsible resp = new Responsible();
						resp.ConpointID = conPointID;
						resp.Name = responsibleOffice.ResponsibleName;
						responsibleDAO.CreateResponsible(resp);
						responsibleID = resp.ResponsibleID;
					}

					responsibleOffice.ResponsibleID = responsibleID;

					ResponsibleOfficeDAO responsibleOfficeDAO = new ResponsibleOfficeDAO();
					responsibleOfficeDAO.CreateResponsibleOffice(responsibleOffice);

					scope.Complete();
				}				
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessCreateResponsibleOfficeException, ex);
			}
		}  

		public static ResponsibleOfficeCollection GetResponsibleOfficesByResponsible(int responsibleID)
		{            
			try
			{
				ResponsibleOfficeDAO responsibleOfficeDAO = new ResponsibleOfficeDAO();
				return responsibleOfficeDAO.GetResponsibleOfficesByResponsible(responsibleID);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetResponsibleOfficesException, ex);
			}
		}  

		public static ResponsibleOfficeCollection GetResponsibleOfficesByConPoint(int conPointID)
		{            
			try
			{
				ResponsibleOfficeDAO responsibleOfficeDAO = new ResponsibleOfficeDAO();
				return responsibleOfficeDAO.GetResponsibleOfficesByConPoint(conPointID);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetResponsibleOfficesException, ex);
			}
		}  

        #region ResponsibleOffice

        public static void CreateResponsibleOffice(ResponsibleOffice responsibleOffice)
        {            
            try
            {
                ResponsibleOfficeDAO responsibleOfficeDAO = new ResponsibleOfficeDAO();
                responsibleOfficeDAO.CreateResponsibleOffice(responsibleOffice);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessCreateResponsibleOfficeException, ex);
            }
        }        

        public static void UpdateResponsibleOffice(ResponsibleOffice responsibleOffice)
        {            
            try
            {
                ResponsibleOfficeDAO responsibleOfficeDAO = new ResponsibleOfficeDAO();
                responsibleOfficeDAO.UpdateResponsibleOffice(responsibleOffice);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessUpdateResponsibleOfficeException, ex);
            }
        }        

        public static void DeleteResponsibleOffice(int responsibleID, int defaultToOfficeID)
        {            
            try
            {
                ResponsibleOfficeDAO responsibleOfficeDAO = new ResponsibleOfficeDAO();
                responsibleOfficeDAO.DeleteResponsibleOffice(responsibleID, defaultToOfficeID);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessDeleteResponsibleOfficeException, ex);
            }
        }
        
        public static ResponsibleOffice GetResponsibleOffice(int responsibleID, int defaultToOfficeID)
        {            
            try
            {
                ResponsibleOfficeDAO responsibleOfficeDAO = new ResponsibleOfficeDAO();
                return responsibleOfficeDAO.GetResponsibleOffice(responsibleID, defaultToOfficeID);                
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetResponsibleOfficeException, ex);
            }
        }                

        public static ResponsibleOfficeCollection GetResponsibleOffices(ResponsibleOfficeColumns orderBy, OrderDirection orderDirection)
        {            
            try
            {
                ResponsibleOfficeDAO responsibleOfficeDAO = new ResponsibleOfficeDAO();
                return responsibleOfficeDAO.GetResponsibleOffices(orderBy, orderDirection);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetResponsibleOfficesException, ex);
            }
        }        

        public static ResponsibleOfficeCollection GetResponsibleOffices(ResponsibleOfficeColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                ResponsibleOfficeDAO responsibleOfficeDAO = new ResponsibleOfficeDAO();
                return responsibleOfficeDAO.GetResponsibleOffices(orderBy, orderDirection, page, pageSize, out totalRecords);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetResponsibleOfficesException, ex);
            }
        }        

        #endregion
    }
}